package com.qianyu.project.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianyu.project.core.bean.Flow;
import com.qianyu.project.core.bean.FlowNode;
import com.qianyu.project.core.mapper.FlowMapper;
import com.qianyu.project.core.service.IFlowNodeService;
import com.qianyu.project.core.service.IFlowService;
import com.qianyu.project.entity.SystemRole;
import com.qianyu.project.service.ISystemRoleService;
import com.qianyu.project.vo.FlowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author gs
 * @date create in 2020/12/11 17:05
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Validated
public class FlowServiceImpl implements IFlowService {

    @Autowired
    private FlowMapper flowMapper;

    @Autowired
    private ISystemRoleService systemRoleService;

    @Autowired
    private IFlowNodeService flowNodeService;


    @Override
    public Flow getByModule(@NotNull Integer moduleId) {
        return flowMapper.selectByModule(moduleId);
    }

    @Override
    public Flow getById(@NotNull Integer flowId) {
        return flowMapper.selecyById(flowId);
    }

    @Override
    public Flow getByName(String flowName) {
        return flowMapper.selectByName(flowName);
    }

    @Override
    public IPage<Flow> list(Map<String, String> param) {
        IPage<Flow> iPage= new Page<>();
        int page =1;
        if (!ObjectUtils.isEmpty(param.get("page"))){
            page=Integer.parseInt(param.get("page"));
        }
        iPage.setSize(5);
        iPage.setCurrent(page);
        return flowMapper.list(iPage,param);
    }

    @Override
    public void save(FlowVo flowVo) {
        Flow ff= flowMapper.selectByflowId(flowVo.getFlow().getFlowId());
        Flow f =flowMapper.selectByNameValidisZone(flowVo.getFlow().getFlowName());
        if(f!=null || ff!=null ){
            Flow flow = new Flow();
            if(ff!=null){
                flowNodeService.delete(ff.getFlowId());
                flow.setFlowId(ff.getFlowId());
            }else{
                flowNodeService.delete(f.getFlowId());
                flow.setFlowId(f.getFlowId());
            }
            flow.setFlowName(flowVo.getFlow().getFlowName());
            flow.setFlowRemark(flowVo.getFlow().getFlowRemark());
            flow.setModuleId(flowVo.getFlow().getModuleId());
            flow.setIsvalid(1);
            flowMapper.update1(flow);

        }else{
            flowMapper.save(flowVo.getFlow());
        }
        List<SystemRole> sr = systemRoleService.list2();
        List<FlowNode> nodeList = flowVo.getNodeList();
        FlowNode flowNode = new FlowNode();
        flowNode.setFlowNodeName("提交申请单");
        flowNode.setFlowNodeRemark("提交申请单");
        if(f!=null || ff!=null){
            if(ff!=null){
                flowNode.setFlowId(ff.getFlowId());
            }else{
                flowNode.setFlowId(f.getFlowId());
            }
        }else {
            flowNode.setFlowId(flowVo.getFlow().getFlowId());
        }
        flowNode.setSeq(1);
        flowNode.setIsvalid(1);
        flowNode.setFlowNodeRole(0);
        nodeList.add(flowNode);
        Collections.sort(nodeList, new Comparator<FlowNode>() {
            @Override
            public int compare(FlowNode o1, FlowNode o2) {
                if(o1.getSeq() > o2.getSeq())
                {
                    return 1;
                }
                else if(o1.getSeq() .equals( o2.getSeq()))
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
        });
        nodeList.forEach(i->{
            sr.forEach(s->{
                if(i.getFlowNodeRole().equals(s.getId())){
                    if(f!=null || ff!=null){
                        if(ff!=null){
                            i.setFlowId(ff.getFlowId());
                        }else{
                            i.setFlowId(f.getFlowId());
                        }
                    }else {
                        i.setFlowId(flowVo.getFlow().getFlowId());
                    }
                    i.setFlowNodeRole(i.getFlowNodeRole());
                    i.setFlowNodeName(s.getRoleName()+"审批");
                    i.setFlowNodeRemark(s.getRoleName()+"审批");
                    i.setIsvalid(1);
                }
            });
        });
        System.out.println(nodeList);

        flowNodeService.saveBatch(nodeList);

    }

    @Override
    public void del(Integer flowId) {
        flowNodeService.update(flowId);
        flowMapper.update(flowId);
    }
}
